import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsNumber,
  IsBoolean,
  IsEnum,
  MaxLength,
  Min
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// 创建菜单 DTO
export class CreateMenuDto {
  @ApiPropertyOptional({
    description: '父菜单ID',
    example: 1
  })
  @IsOptional()
  @IsNumber()
  parentId?: number;

  @ApiPropertyOptional({
    description: '菜单类型',
    example: 1,
    enum: [0, 1, 2],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1, 2])
  type?: number;

  @ApiProperty({
    description: '菜单标题',
    example: '用户管理',
    maxLength: 100
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  title: string;

  @ApiPropertyOptional({
    description: '国际化键',
    example: 'menu.user.management',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  i18nKey?: string;

  @ApiProperty({
    description: '菜单名称',
    example: 'UserManagement',
    maxLength: 100
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  name: string;

  @ApiPropertyOptional({
    description: '路径',
    example: '/admin/users',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  path?: string;

  @ApiPropertyOptional({
    description: '组件路径',
    example: '/admin/users/index',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  component?: string;

  @ApiPropertyOptional({
    description: '权限',
    example: 'admin:user:list',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  permission?: string;

  @ApiPropertyOptional({
    description: '图标',
    example: 'i-carbon-user',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  icon?: string;

  @ApiPropertyOptional({
    description: '图标类型',
    example: 'iconify',
    maxLength: 50,
    default: 'iconify'
  })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  iconType?: string;

  @ApiPropertyOptional({
    description: '排序',
    example: 1,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  order?: number;

  @ApiPropertyOptional({
    description: '是否外链',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  isExt?: boolean;

  @ApiPropertyOptional({
    description: '外链打开方式',
    example: '_blank',
    maxLength: 50,
    default: '_blank'
  })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  extOpenMode?: string;

  @ApiPropertyOptional({
    description: '外链地址',
    example: 'https://example.com',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  href?: string;

  @ApiPropertyOptional({
    description: '是否缓存',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  keepAlive?: boolean;

  @ApiPropertyOptional({
    description: '是否在菜单中隐藏',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  hideInMenu?: boolean;

  @ApiPropertyOptional({
    description: '激活菜单',
    example: 'UserList',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  activeMenu?: string;

  @ApiPropertyOptional({
    description: '是否支持多标签',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  multiTab?: boolean;

  @ApiPropertyOptional({
    description: '标签页固定索引',
    example: 0
  })
  @IsOptional()
  @IsNumber()
  fixedIndexInTab?: number;

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;
}

// 更新菜单 DTO
export class UpdateMenuDto extends PartialType(CreateMenuDto) {}

// 查询菜单 DTO
export class QueryMenusDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 50,
    minimum: 1,
    maximum: 100,
    default: 50
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  limit?: number = 50;

  @ApiPropertyOptional({
    description: '搜索关键词（标题、名称、路径）',
    example: '用户'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '菜单类型',
    enum: [0, 1, 2]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1, 2])
  type?: number;

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '父菜单ID',
    example: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  parentId?: number;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'order',
    enum: ['createdAt', 'updatedAt', 'title', 'order']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'order';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'ASC',
    enum: ['ASC', 'DESC'],
    default: 'ASC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'ASC';
}

// 更新菜单排序 DTO
export class UpdateMenuOrderDto {
  @ApiProperty({
    description: '菜单ID和排序的映射',
    example: [{ id: 1, order: 1 }, { id: 2, order: 2 }]
  })
  @IsOptional()
  menuOrders: Array<{ id: number; order: number }>;
}
